import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    component: () => import(/* webpackChunkName: "about" */ '../views/Login.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName: "about" */ '../views/Login.vue')
  },
  {
    path: '/forgetPasswordView',
    name: 'forgetPasswordView',
    component: () => import(/* webpackChunkName: "about" */ '../views/ForgetPasswordView.vue')
  },
  {
    path: '/register',
    name: 'register',
    component: () => import(/* webpackChunkName: "about" */ '../views/Register.vue')
  },
  {
    path: '/answerStatistic',
    name: 'answerStatistic',
    component: () => import(/* webpackChunkName: "about" */ '../views/AnswerStatistic.vue')
  },
  {
    path: '/questionnaireList',
    name: 'questionnaireList',
    component: () => import(/* webpackChunkName: "about" */ '../views/QuestionnaireList.vue'),
    meta: {
      requireAuth:true,
    },
  },
  {
    path: '/dataScreen',
    name: 'dataScreen',
    component: () => import(/* webpackChunkName: "about" */ '../views/DataScreen.vue')
  },
  {
    path: '/recycleBin',
    name: 'recycleBin',
    component: () => import(/* webpackChunkName: "about" */ '../views/RecycleBin.vue'),
    meta: {
      requireAuth:true,
    },
  },
  {
    path: '/chooseTemplate',
    name: 'chooseTemplate',
    component: () => import(/* webpackChunkName: "about" */ '../views/ChooseTemplate.vue'),
    meta: {
      requireAuth:true,
    },
  },
  {
    path: '/answerQuestionnaire',
    name: 'answerQuestionnaire',
    component: () => import(/* webpackChunkName: "about" */ '../views/AnswerQuestionnaire.vue'),
    meta: {
      requireAuth:true,
    },
  },
  {
    path: '/groupList',
    name: 'groupList',
    component: () => import(/* webpackChunkName: "about" */ '../views/GroupList.vue'),
    meta: {
      requireAuth:true,
    },
  },
  {
    path: '/answer',
    name: 'answer',
    component: () => import(/* webpackChunkName: "about" */ '../views/Answer.vue')
  },
  {
    path: '/publish',
    name: 'publish',
    component: () => import(/* webpackChunkName: "about" */ '../views/Publish.vue')
  },
  {
    path: '/createQuestionnaire',
    name: 'createQuestionnaire',
    component: () => import(/* webpackChunkName: "about" */ '../views/CreateQuestionnaire.vue')
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth) {  // 判断该路由是否需要登录权限
    if (sessionStorage.getItem('userId')) {  // 获取用户登录信息
      next();
    }else {
      alert("请先登录")
      next({
        path: '/login',
        query: {redirect: to.fullPath}, // 将跳转的路由path作为参数，登录成功后跳转到该路由
      })
    }
  }
  else {
    next();
  }
})
export default router
